package org.wtframework.platform.server.model.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Proxy;
import org.openredhead.springx.core.server.model.IGroup;

@Entity
@Proxy(lazy = true)
@DynamicInsert(value = true)
@DynamicUpdate(value = true)
@Table(name = "orh_sx_group")
@SecondaryTable(name = "orh_sx_link_group_group", pkJoinColumns = { @PrimaryKeyJoinColumn(name = "child_id") })
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Group implements IGroup {

	private static final long serialVersionUID = 1394877934723481972L;

	private Integer id;

	private String identifier;

	private String name;

	private String descn;

	private Date establishDate = new Date();

	private List<Integer> rolesId;

	private Integer parentId;

	private List<Integer> childsId;

	private Integer orderId;

	private Integer sortId = 1; //部门类别，默认为用1
	
	public void setId(Integer id) {
		this.id = id;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Integer getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	public String getDescn() {
		return descn;
	}

	public void setEstablishDate(Date establishDate) {
		this.establishDate = establishDate;
	}

	@Column(name = "establish_date")
	public Date getEstablishDate() {
		return establishDate;
	}

	public void setRolesId(List<Integer> rolesId) {
		this.rolesId = rolesId;
	}

	@ElementCollection(fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	@JoinTable(name = "orh_sx_link_group_role", joinColumns = @JoinColumn(name = "group_id"))
	@Column(name = "role_id")
	public List<Integer> getRolesId() {
		return rolesId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	@Column(table = "orh_sx_link_group_group", name = "parent_id")
	public Integer getParentId() {
		return parentId;
	}

	public void setChildsId(List<Integer> childsId) {
		this.childsId = childsId;
	}

	@ElementCollection(fetch = FetchType.LAZY)
	@Fetch(FetchMode.SUBSELECT)
	@JoinTable(name = "orh_sx_link_group_group", joinColumns = @JoinColumn(name = "parent_id"))
	@Column(name = "child_id")
	public List<Integer> getChildsId() {
		return childsId;
	}

	public void setOrderId(Integer orderId) {
		this.orderId = orderId;
	}

	@Column(name = "order_id")
	public Integer getOrderId() {
		return orderId;
	}

	@Column(name = "sort_id")
	public Integer getSortId() {
		return sortId;
	}

	
	public void setSortId(Integer sortId) {
		this.sortId = sortId;
	}

	@Override
	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	@Override
	public String getIdentifier() {
		return identifier;
	}

	@Override
	public boolean equals(Object o) {

		if (o == null) return false;

		if (!(o instanceof Group)) return false;

		final Group instance = (Group) o;

		if (getId() != null ? !getId().equals(instance.getId()) : instance.getId() != null) return false;

		return true;

	}

	@Override
	public int hashCode() {

		return (getId() != null ? getId().hashCode() : 0);

	}

}
